package com.brest.managesambafilesharing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.brest.managesambafilesharing.IsSambaRunning.OnResponseListenerIsSambaRunning;
import com.brest.tools.RunAsRoot;
import com.brest.tools.RunAsRoot.OnResponseListenerRunAsRoot;

public class MainActivity extends Activity {
	private ProgressBar mProgressBarSearch;
	private TextView mStatustextView;
	private ImageButton mRefreshStatusImageButton;
	private ImageButton mStartImageButton;
	private ImageButton mStopImageButton;
	public final static MyNotificationMgr myNotificationMgr = new MyNotificationMgr();
	private int ToastTime = 2;

	protected OnResponseListenerRunAsRoot onResponseListener = new OnResponseListenerRunAsRoot() {
		public void onResponse(String message) {
			/*
			 * AlertDialog.Builder builder = new
			 * AlertDialog.Builder(GeneralHTTPRequestExampleActivity.this);
			 * builder.setPositiveButton("Okay", new
			 * DialogInterface.OnClickListener() { public void
			 * onClick(DialogInterface dialog, int which) { dialog.cancel(); }
			 * }); builder.setMessage("Failure. Message: " + message);
			 * builder.show(); }
			 */
			// Toast.makeText(MainActivity.this.getApplicationContext(),
			// message, ToastTime).show();
			startActivity(new Intent(MainActivity.this.getApplicationContext(),
					MainActivity.class).setAction("INTENTINFO_STATUS"));

		}
	};

	protected OnResponseListenerIsSambaRunning onResponseListenerIsSambaRunning = new OnResponseListenerIsSambaRunning() {
		public void onResponse(String message) {
			try {
				Toast.makeText(MainActivity.this, message, ToastTime).show();

				MainActivity.this.mStatustextView.setText(message);
				MainActivity.this.mProgressBarSearch
						.setVisibility(View.INVISIBLE);
				// SambaFileSharingUpdate.this.mProgressBarSearchSamba.setProgress(0);
				new MyNotificationMgr().createNotification(
						MainActivity.this.getApplicationContext(), message);
			} catch (Exception e) {
				LOG.e("onPostExecute error : " + e.getMessage());
			}
		}
	};

	private void startSamba() {
		LOG.d("startSamba() ");
		String commands[] = { SambaCmdToolBean.STARTSAMBA };
		new RunAsRoot().start(commands, onResponseListener);
	}

	private void stopSamba() {
		LOG.d("stopSamba() ");
		String commands[] = { SambaCmdToolBean.STOPSAMBA };
		new RunAsRoot().start(commands, onResponseListener);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LOG.i("onCreate");
		if ("INTENTINFO_CLOSE".equals(getIntent().getAction())) {
			LOG.i("onCreate INTENTINFO_CLOSE");
			this.setTheme(R.style.EmptyActivity);
		} else {
			LOG.i("onCreate != INTENTINFO_CLOSE");
			this.setTheme(R.style.AppTheme);
			setContentView(R.layout.activity_main);

			mProgressBarSearch = (ProgressBar) this
					.findViewById(R.id.progressBar1);
			mProgressBarSearch.setVisibility(View.INVISIBLE);

			mStatustextView = (TextView) this.findViewById(R.id.statustextView);

			mRefreshStatusImageButton = (ImageButton) this
					.findViewById(R.id.reloadSambaStateButton1);
			mRefreshStatusImageButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					new IsSambaRunning(onResponseListenerIsSambaRunning)
							.execute("status");
				}
			});

			mStartImageButton = (ImageButton) this
					.findViewById(R.id.startButton);
			mStartImageButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					startSamba();
				}
			});
			mStopImageButton = (ImageButton) this.findViewById(R.id.stopButton);
			mStopImageButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					stopSamba();
				}
			});
		}
		handleIntent(this.getIntent());

	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		/*
		 * MainActivity.myNotificationMgr
		 * .createNotification(getApplicationContext());
		 */
		// new IsSambaRunning().execute("status");

		// Intent serviceIntent = new Intent();
		// serviceIntent.setClassName("com.brest.managesambafilesharing",
		// "com.brest.managesambafilesharing.MyNotificationMgrService");
		// getApplicationContext().startService(serviceIntent);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { //
		case R.id.menu_settings:
			startActivity(new Intent(this, SettingsActivity.class)); //
			break;
		}
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	protected void handleIntent(Intent intent) {
		String actionName = intent.getAction();
		LOG.d("onNewIntent : " + actionName);
		if ("INTENTINFO_START".equals(actionName)) {
			startSamba();
		}
		if ("INTENTINFO_STOP".equals(actionName)) {
			stopSamba();
		}
		if ("INTENTINFO_STATUS".equals(actionName)
				|| "android.intent.action.MAIN".equals(actionName)) {
			new IsSambaRunning(onResponseListenerIsSambaRunning) {
				protected void onPreExecute() {
					LOG.i("Pre-Execute");
					super.onPreExecute();
					MainActivity.this.mStatustextView.setText("");
					MainActivity.this.mProgressBarSearch
							.setVisibility(View.VISIBLE);
				}
			}.execute("status");
		}
		if ("INTENTINFO_CLOSE".equals(actionName)) {
			this.finish();
		}

	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		handleIntent(intent);
	}

	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
		MainActivity.myNotificationMgr
				.deleteNotification(getApplicationContext());
	}

}
